module.exports = function ($scope, $modalInstance, $resource, $modal, item, flag, toaster){
    

    //查询type列表
    $scope.search = function (){
        var para = {
            'type': '3',
            'state': '1'
        }
        $resource('/api/as/goc/pelawinfo/findTypeList', {}, {}).save(para, function(res){
            if(res.errcode === 0){
                $scope.typelist = res.data.results;
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

    //获取模板信息
    function getInfo(){
        
        var para = {
           'id' : $scope.templan.id
        }
        $resource('/api/as/goc/peopentemplan/getInfo', {}, {}).save(para, function(res){
            if(res.errcode === 0){
                $scope.info = res.data;
                console.log($scope.info);
                $scope.date.lable = $scope.info.start_date;
                $scope.date1.lable = $scope.info.end_date;
                $scope.templan.travel_name = change1($scope.info.travel_name);
                $scope.templan.travel_address = change1($scope.info.travel_address);
                $scope.templan.travel_license = change1($scope.info.travel_license);
                $scope.templan.travel_license_no = change1($scope.info.travel_license_no);
                $scope.templan.travel_legal_person_name = change1($scope.info.travel_legal_person_name);
                $scope.templan.travel_legal_person_no = change1($scope.info.travel_legal_person_no);
                $scope.templan.business_place = change1($scope.info.business_place);
                $scope.templan.licensing_scope = change1($scope.info.licensing_scope);
                $scope.templan.registered_capital = change1($scope.info.registered_capital);
                $scope.templan.setup_time = change1($scope.info.setup_time);
                $scope.templan.TEL = change1($scope.info.TEL);
                $scope.templan.discipline_info = change1($scope.info.discipline_info);
               
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
        
    }

    function date2str(d) {
        if (d === undefined) {
            return "";
        }
        var month = (d.getMonth() + 1).toString();
        var day = d.getDate().toString();
        if (month.length == 1) month = '0' + month;
        if (day.length == 1) day = '0' + day;
        return d.getFullYear() + "-" + month + "-" + day;
    }

    $scope.date = {
        //  'lable': date2str(new Date()),
        //  'value': date2str(new Date()),
    // 'lable': date2str2(new Date()),
            'opened': false
        }


         $scope.dateOpen = function ($event, item) {
            $event.preventDefault();
            $event.stopPropagation();
            item.opened = true;
        };

        $scope.date1 = {
            //   'lable': date2str(new Date()),
            //  'value': date2str(new Date()),
            'opened': false
        }


         $scope.dateOpen1 = function ($event, item) {
            $event.preventDefault();
            $event.stopPropagation();
            item.opened = true;
        };
    init();
    function init(){
        $scope.templan =item;
        if($scope.templan){
            getInfo();
            console.log($scope.templan);
        }
        $scope.search();
    }
    //保存
    $scope.save = function (){
        // if($scope.templan.type == undefined){
        //     toaster.error({title: '', body: '请选择旅游法规信息类别！'});
        //   return;
        // }
        // if($scope.templan.info == undefined){
        //     toaster.error({title: '', body: '请输入旅游法规信息详情！'});
        //   return;
        // }
        // if($scope.templan.law_title == undefined){
        //     toaster.error({title: '', body: '请输入旅游法规信息标题！'});
        //   return;
        // }
        var para = {
            'templan_title': $scope.templan.templan_title,
            'start_date': date2str($scope.date.lable) + ' 00:00:00',
            'end_date' : date2str($scope.date1.lable) + ' 00:00:00',
            'id' :  $scope.templan.id,
            'type' :  $scope.templan.type,
            'travel_name' :  change($scope.templan.travel_name),
            'travel_address' : change($scope.templan.travel_address),
            'travel_license' :  change($scope.templan.travel_license),
            'travel_license_no' :  change($scope.templan.travel_license_no),
            'travel_legal_person_name' : change($scope.templan.travel_legal_person_name),
            'travel_legal_person_no' : change($scope.templan.travel_legal_person_no),
            'business_place' : change($scope.templan.business_place),
            'licensing_scope' : change($scope.templan.licensing_scope),
            'registered_capital' :  change($scope.templan.registered_capital),
            'setup_time' :  change($scope.templan.setup_time),
            'TEL' : change($scope.templan.TEL),
            'discipline_info' : change($scope.templan.discipline_info)
        }
       
        $resource('/api/as/goc/peopentemplan/save', {}, {}).save(para, function(res){
            if(res.errcode === 0){
                toaster.success({title: '', body: '保存成功'});
                $modalInstance.close();
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }
   
    //取消
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    }


    function change(info) {
        if(info){
            return '1';
        }else{
            return '0';
        }
    }

    function change1(info) {
        if(info == 1){
            return true;
        }else{
            return false;
        }
    }
};